//
//  HBHomeTableViewCell.swift
//  sinaWeibo
//
//  Created by Beans on 16/6/26.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit
import SnapKit

class HBHomeTableViewCell: UITableViewCell {

    // (1) 定义约束成员变量 (记录底部约束)
    var bottomConstraint: Constraint?

    var model: HBStatusCellViewModel? {
        didSet {
            printLog(model?.model?.source)
            printLog(model?.model?.created_at)

            orginnalView.originalViewModel = model
            footerView.footerViewModel = model

            // 通过修改footerView的顶部去影响自动计算行高, 达到没有转发微博
            // 问题:
            // ① cell的重用
            // ② 约束的不唯一 (view, toView, attributes)
            // addConstraint(NSLayoutConstraint(item: AnyObject, attribute: NSLayoutAttribute, relatedBy: NSLayoutRelation, toItem: AnyObject?, attribute: NSLayoutAttribute, multiplier: CGFloat, constant: CGFloat))

            // (3) 每次赋值/判断之前, 先把之前的约束卸载
            bottomConstraint?.uninstall()

            // 说明没有转发微博内容
            if model?.model?.retweeted_status == nil {
                printLog("原创微博")
                repostView.hidden = true
                // 更新约束
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    // (4) 再次管理, 为下次cell的重用做准备
                    self.bottomConstraint = make.top.equalTo(orginnalView.snp_bottom).constraint
                })

            } else {
                printLog("转发微博")
                repostView.hidden = false
                // 更新约束
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    // (4) 再次管理, 为下次cell的重用做准备
                    self.bottomConstraint = make.top.equalTo(repostView.snp_bottom).constraint
                })
                // 赋值
                repostView.repostViewModel = model
            }
        }
    }

    // 1> 重写构造函数
    // Cell的指定构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 2> 添加控件与布局
    private func setupUI() {

        // 添加子控件
        contentView.addSubview(orginnalView)
        contentView.addSubview(footerView)
        contentView.addSubview(repostView)

        // 设置约束
        orginnalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            // B
            make.bottom.equalTo(repostView.snp_top)
        }

        repostView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            // T
//            make.top.equalTo(orginnalView.snp_bottom)
        }

        footerView.snp_makeConstraints { (make) -> Void in

            // (2) 关联控件
            self.bottomConstraint = make.top.equalTo(repostView.snp_bottom).constraint

            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            // B
            make.bottom.equalTo(contentView.snp_bottom)
        }

        // 自动计算行高 (contentView, 尤其是底部)
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            // 最重要的是底部
            make.bottom.equalTo(footerView.snp_bottom)
        }

    }

    // 3> 懒加载
    private lazy var orginnalView: HBOriginalView = HBOriginalView()
    private lazy var footerView: HBToolBarView = HBToolBarView()
    private lazy var repostView: HBRepostView = HBRepostView()
    
}
